package cn.hu.org.controller;

import cn.hu.basic.util.ExcelUtil;
import cn.hu.org.domain.ShopAuditLog;
import cn.hu.org.service.IShopService;
import cn.hu.org.domain.Shop;
import cn.hu.org.query.ShopQuery;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        if ( shop.getId() != null){
            shopService.update(shop);
        }else{
            shopService.add(shop);
        }
        return AjaxResult.getInstance();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        shopService.delete(id);
        return AjaxResult.getInstance();
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list() {
        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.listByQuery(query);
    }

    @PostMapping("/settle")
    public AjaxResult settle(@RequestBody @Valid Shop shop){
        shopService.settle(shop);
        return AjaxResult.getInstance();
    }

    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        shopService.pass(log);
        return AjaxResult.getInstance();
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        shopService.reject(log);
        return AjaxResult.getInstance();
    }

    @GetMapping("/export_excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺",Shop.class,"店铺信息.xls",response);
    }
    @PostMapping("/import_excel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    /**
     * 店铺状态统计
     * @return
     */
    @GetMapping("/statistics")
    public AjaxResult statistics(){
        return shopService.statistics();
    }
}
